/*
 * @Author: lee
 * @Date: 2023-10-25 16:00:27
 * @LastEditors: lee
 * @LastEditTime: 2023-10-25 19:28:11
 * @Description: file content
 */
const {
	userModel
} = require('../models/user.model')

const {
	setToken,
	getToken
} = require('../utils/jwt')

const {
	md5
} = require('../utils/md5')

module.exports = class User {
	//注册
	static async addUser(ctx) {
		let {
			username,
			password,
			phone,
			email,
			profile,
			auth,
		} = ctx.request.body

		if (!(username || phone || email) && password) {
			return ctx.fail({
				msg: '有未填项',
				code: 401
			})
		}

		// 判断是用什么注册的
		let ico = username ? ['username', '账号', username] : phone ? ['phone', '手机号', phone] : email ? ['email',
			'邮箱', email
		] : '';
		// 判断账号是否存在
		let bool = await userModel.find({
			[ico[0]]: ico[2]
		});
		if (bool.length) {
			return ctx.fail({
				msg: ico[1] + '已被注册',
				code: 401
			})
		}

		// 添加用户信息
		let data = await userModel.insertMany({
			username,
			password:md5(password),
			phone,
			email,
			profile,
			auth
		})

		if (data.length) {
			return ctx.success({
				msg: '注册成功'
			})
		} else {
			return ctx.fail({
				msg: '添加失败'
			})
		}
	}

	//登录
	static async loginUser(ctx) {
		let {
			username,
			password,
			phone,
			email,
			verifyCode
		} = ctx.request.body;
		const code = ctx.session.verifyCode;
		// 是否有必填项未填
		if (!((username || phone || email) && password && verifyCode)) {
			return ctx.fail({
				msg: '有未填项',
				code: 401
			})
		}
		const _verifyCode = verifyCode.toLocaleLowerCase()
		if (_verifyCode != code) {
			return ctx.fail({
				msg: '验证码不正确',
				code: 401
			})
		}
		// 判断是用什么注册的
		let ico = username ? ['username', '账号', username] : phone ? ['phone', '手机号', phone] : email ? ['email',
			'邮箱', email
		] : '';
		// 获取用户信息
		let data = await userModel.find({
			[ico[0]]: ico[2]
		});

		// 没有注册
		if (!data.length) {
			return ctx.fail({
				msg: ico[1] + '未注册',
				code: 201
			})
		}
		// 密码不正确
		const md5Password = md5(password);
		if (md5Password !== data[0].password) {
			return ctx.fail({
				msg: ico[1] + '或密码错误',
				code: 401
			})
		}

		let token = setToken(data[0]._id);

		const userInfo = data[0].toAuthJSON()

		return ctx.success({
			msg: '登录成功',
			data: {
				user: userInfo,
				token
			}
		})

	}

	//获取当前用户

	//拓展模糊查询，名字查询，邮箱查询获取用户等
	static async currentUser(ctx) {

		let query = {}

		if (ctx.request.query.id != undefined) {
			query._id = {
				$in: [ctx.request.query.id]
			}
		}


		if (ctx.request.query.username != undefined) {
			query.username = {
				$in: [ctx.request.query.username]
			}
		}
		if (ctx.request.query.email != undefined) {
			query.email = {
				$in: [ctx.request.query.email]
			}
		}

		const data = await userModel.find(query);
		const user = data[0]
		if (!user) {
			return ctx.fail({
				msg: '用户不存在',
				code: 401
			})
		} else {
			return ctx.success({
				msg: '获取成功',
				data: {
					user:user.toAuthJSON(ctx.user.id)
				}
			})
		}
	}
	static async updateUser(ctx) {

		const newUser = ctx.request.body;

		if (ctx.user.id != newUser.id) {
			return ctx.fail({
				msg: '暂无权限'
			})
		}

		const user = await userModel.findById(newUser.id);

		if (newUser.username != undefined) {
			user.username = newUser.username;
		}
		if (newUser.email != undefined) {
			user.email = newUser.email;
		}
		if (newUser.note != undefined) {
			user.note = newUser.note;
		}
		if (newUser.profile != undefined) {
			user.profile = newUser.profile;
		}
		if (newUser.password != undefined) {
			user.password = md5(newUser.password);
		}

		await user.save();
		return ctx.success({
			msg: '修改成功',
			data: {
				user:user.toAuthJSON()
			}
		})
	}

	//删除用户
	static async deleteUser(ctx) {
		if (ctx.body.data[0].id != ctx.request.body.id) {
			return ctx.fail({
				msg: '暂无权限',
				code: 401
			})
		}
		const res = await userModel.deleteOne({
			_id: ctx.request.body.id
		});
		if (res.ok == 1) {
			return ctx.success({
				msg: '删除成功'
			})
		}
	}

	//获取全部用户
	static async allUser(ctx) {
		let page = 1;
		let size = 10

		let users = []

		if (typeof ctx.request.query.size != undefined) {
			size = ctx.request.query.size;
		}

		if (typeof ctx.request.query.page != undefined) {
			page = ctx.request.query.page;
		}
		users = await userModel.find()
			.skip(Number(page - 1) * Number(size))
			.limit(Number(size))
			.sort({
				creatAt: -1
			})

		users = users.map(user => user.toAuthJSON())

		if (ctx.user) {
			const authUser = await userModel.findById(ctx.user.id);
			users = users.map(user => user.toAuthJSON(authUser))
		}
		return ctx.success({
			msg: '获取成功',
			data: {
				users,
				total: users.length
			}
		})
	}

	//关注用户

	static async addFollower(ctx) {
		const id = ctx.request.body.id;
		if (id == ctx.user.id) {
			return ctx.fail({
				msg: '不能自己关注自己'
			})
		}
		const user = await userModel.findById(id)
		user.favorited = user.assertFollower(ctx.user.id)
		return ctx.success({
			msg: '关注成功'
		})
	}

	//取消关注
	static async unFollower(ctx) {
		const id = ctx.request.body.id;
		if (id == ctx.user.id) {
			return ctx.fail({
				msg: '不能自己关注自己'
			})
		}
		const user = await userModel.findById(id)
		user.favorited = user.unFollower(ctx.user.id)
		return ctx.success({
			msg: '取消关注成功'
		})
	}

}
